//
//  LGRecoverySportsController.m
//  HeartDiseaseManage
//
//  Created by 1234 on 2023/7/31.
//  康复方案--运动

#import "LGRecoverySportsController.h"
//创建、编辑、复制 - 运动方案
#import "LGSportsPlanCreateController.h"
//运动方案详情
#import "LGSportsPlanDetailController.h"

#import "LGRecoverySportsCell.h"
@interface LGRecoverySportsController ()<UITableViewDelegate,UITableViewDataSource,LGRecoverySportsCellDelegate>
/**   */
@property (nonatomic , strong)UITableView *tableView;
/**   */
@property (nonatomic , strong)UILabel *countLabel;
/**   */
@property (nonatomic , strong)UIButton *createBtn;
/**   */
@property (nonatomic , strong)NSMutableArray *dataArray;
/**   */
@property (nonatomic , copy)NSString *pageNo;

@end

@implementation LGRecoverySportsController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    self.pageNo = @"1";
    self.title = @"运动方案";
    [self.view addSubview:self.tableView];
    [self.view addSubview:self.createBtn];
    [self emptyViewWithSuperView:self.view frame:self.tableView.frame];
    [self requestData];
}
-(void)requestData{
    self.pageNo = @"1";
    self.dataArray = [NSMutableArray array];
    [self requestMoreData];
}

-(void)requestMoreData{
    [RequestUtil withPOST:@"/doctor/sport.plan/pagedList" parameters:@{@"pageNo":self.pageNo,@"pageSize":@"20"} withSign:YES success:^(NSInteger code, NSString *message, id returnData) {
        self.isfirst = NO;
        [self.emptyView stopBufferAction];
        [self.tableView.mj_header endRefreshing];
        [self.tableView.mj_footer endRefreshing];
        if(code == 200){
            self.countLabel.text = [NSString stringWithFormat:@"运动方案：%@个",returnData[@"total"]];
            NSArray *tempArray = returnData[@"data"];
            if(tempArray.count>0){
                self.pageNo = [NSString stringWithFormat:@"%ld",[returnData[@"current_page"] integerValue]+1];
                [self.dataArray addObjectsFromArray:tempArray];
            }else{
                [self.tableView.mj_footer endRefreshingWithNoMoreData];
            }
        }
        [self.tableView reloadData];
        if(self.dataArray.count == 0){
            [self.emptyView showViewWithImage:@"emptyView2" content:@"暂无运动方案" offset:0];
        }
    } error:^(NSError *error) {
        self.isfirst = NO;
        [self.emptyView stopBufferAction];
        [self.tableView.mj_header endRefreshing];
        [self.tableView.mj_footer endRefreshing];
        if(self.dataArray.count == 0){
            [self.emptyView showViewWithImage:@"emptyView2" content:@"暂无运动方案" offset:0];
        }
    }];
}

//创建运动方案
-(void)createSportsProgramAction{
    LGSportsPlanCreateController *controller = [[LGSportsPlanCreateController alloc]init];
    [self.navigationController pushViewController:controller animated:YES];
    __weak typeof(self) weakSelf = self;
    controller.saveDataSuccess = ^{
        [weakSelf requestData];
    };
}

//删除
-(void)deleteSportsRecovery:(NSString *)dataid index:(NSInteger)index{
    __block NSString *ids = dataid;
    __block NSInteger currentIndex = index;
    __weak typeof(self) weakSelf = self;
    [LGAlertTool alertWithTitle:@"删除运动方案" content:@"删除后不影响已发放的患者，是否确认删除？" sureAction:^{
        [RequestUtil withPOST:@"/doctor/sport.plan/delete" parameters:@{@"ids":ids} withSign:YES success:^(NSInteger code, NSString *message, id returnData) {
            if(code == 200){
                [TooltipView showMessage:@"删除成功" offset:0];
                [weakSelf.dataArray removeObjectAtIndex:currentIndex];
                [weakSelf.tableView reloadData];
                weakSelf.countLabel.text = [NSString stringWithFormat:@"运动方案：%@个",returnData[@"remaining"]];
                if(weakSelf.dataArray.count == 0){
                    [weakSelf.emptyView showViewWithImage:@"emptyView2" content:@"暂无运动方案" offset:0];
                }
            }else{
                [TooltipView showMessage:message offset:0];
            }
        } error:^(NSError *error) {
            [TooltipView showMessage:@"删除失败，请重试" offset:0];
        }];
    }];
    
}
//复制
-(void)copySportsRecovery:(NSDictionary *)dataDic{
    LGSportsPlanCreateController *controller = [[LGSportsPlanCreateController alloc]init];
    controller.copyid = LGNSString(dataDic[@"id"]);
    [self.navigationController pushViewController:controller animated:YES];
    __weak typeof(self) weakSelf = self;
    controller.saveDataSuccess = ^{
        [weakSelf requestData];
    };
}

-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return self.dataArray.count;
}

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    LGRecoverySportsCell *cell = [tableView dequeueReusableCellWithIdentifier:@"cell" forIndexPath:indexPath];
    cell.selectionStyle = UITableViewCellSelectionStyleNone;
    cell.dataDic = self.dataArray[indexPath.row];
    cell.index = indexPath.row;
    cell.delegate = self;
    return cell;
}

-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    NSDictionary *tempDic = self.dataArray[indexPath.row];
    LGSportsPlanDetailController *controller = [[LGSportsPlanDetailController alloc]init];
    controller.planid = LGNSString(tempDic[@"id"]);
    [self.navigationController pushViewController:controller animated:YES];
    __weak typeof(self) weakSelf = self;
    controller.sportsPlanChanged  = ^{
        [weakSelf requestData];
    };
}

-(void)viewDidLayoutSubviews{
    [super viewDidLayoutSubviews];
    self.tableView.frame = CGRectMake(0, 0, Screen_W, self.view.bounds.size.height-viewPix(82)-bottomSafeBarHeight/2.0);
    self.createBtn.frame = CGRectMake(viewPix(16), kMaxY(self.tableView.frame)+viewPix(12), Screen_W-viewPix(32), viewPix(48));
}

-(NSMutableArray *)dataArray{
    if(!_dataArray){
        _dataArray = [NSMutableArray array];
    }
    return _dataArray;
}

-(UITableView *)tableView{
    if(!_tableView){
        _tableView = [[UITableView alloc] initWithFrame:CGRectMake(0, 0, Screen_W, Screen_H-topBarHeight-viewPix(82)-bottomSafeBarHeight/4.0) style:UITableViewStyleGrouped];
        [_tableView registerClass:[LGRecoverySportsCell class] forCellReuseIdentifier:@"cell"];
        _tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
        _tableView.backgroundColor = [UIColor clearColor];
        _tableView.showsVerticalScrollIndicator = NO;
        _tableView.rowHeight = viewPix(164);
        _tableView.dataSource = self;
        _tableView.delegate = self;
        _tableView.tableHeaderView = [self tableHeaderView];
        _tableView.mj_header = [MJRefreshNormalHeader headerWithRefreshingTarget:self refreshingAction:@selector(requestData)];
        _tableView.mj_footer = [MJRefreshBackNormalFooter footerWithRefreshingTarget:self refreshingAction:@selector(requestMoreData)];
    }
    return _tableView;
}

-(UIButton *)createBtn{
    if(!_createBtn){
        _createBtn = [UIButton buttonWithTitle:@"创建运动方案" topMargin:kMaxY(self.tableView.frame)+viewPix(12)];
        [_createBtn addTarget:self action:@selector(createSportsProgramAction) forControlEvents:UIControlEventTouchUpInside];
    }
    return _createBtn;
}

-(UILabel *)countLabel{
    if(!_countLabel){
        _countLabel = [UILabel lableWithText:@"运动方案：" colorString:@"#333333" textFont:LGFont(16) textAlignment:NSTextAlignmentLeft lines:1];
        _countLabel.frame = CGRectMake(viewPix(18), viewPix(5), viewPix(200), viewPix(45));
    }
    return _countLabel;
}

-(UIView *)tableHeaderView{
    UIView *baseView = [[UIView alloc]initWithFrame:CGRectMake(0, 0, Screen_W, viewPix(50))];
    baseView.backgroundColor = [UIColor colorWithString:@"#F9F9F9"];
    [baseView addSubview:self.countLabel];
    return baseView;
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
